package com.liyy.leetcode.editor.cn;

/**
 * @author yinye
 * @since 2020-12-08 17:42
 */
public class test {
    public static void main(String[] args) {

        String str = " aaaaaaaa bc aaaa aaa bbbbasd";

        int result = 0;
        int start = 0;
        int end = 0;
        int lastStartIndex = 0;
        while (end < str.length()){
            if (str.charAt(end) == ' ') {
                result = Math.max(result,end - start);
                start = end;
                start++;
                lastStartIndex = start;
            }
            end++;
        }

        //处理最长字符串在最右侧的情况
        int lastStrLength = str.length() - lastStartIndex;
        result = (result > lastStrLength ? result : lastStrLength);

        System.out.println(result);
    }

    public void minSubArrayLen2() {
        String str = "aa bc aaaa aaa";

        int cns = Integer.MAX_VALUE;

        int start = 0;
        int end = 0;
        while (end < str.length()){
            while (end == ' ') {
                cns = Math.min(cns,end - start + 1);
                start = end;
            }
            end++;
        }

         int result = cns == Integer.MAX_VALUE ? 0 : cns;

        System.out.println(cns);
    }

    public int minSubArrayLen(int s, int[] nums) {
        int cns = Integer.MAX_VALUE;

        int start = 0;
        int end = 0;
        int sum = 0;
        while (end < nums.length){
            sum += nums[end];
            while (sum >= s) {
                cns = Math.min(cns,end - start + 1);
                sum-=nums[start];
                start++;
            }
            end++;
        }

        return cns == Integer.MAX_VALUE ? 0 : cns;
    }
}
